package vn.game;

import vn.game.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.util.Log;

import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

//import android.widget.TableRow.LayoutParams;

public class CrosswordLayout extends TableLayout {

	private String tag = CrosswordLayout.class.getSimpleName();
	// private int column, row;
	private Context context;
//	private int row, column;
	private PlayGame playGame;
	OnTouchEditText touchEdit;
	int widthCell;

	// Cell
	// public int idEdit = 0;

	public CrosswordLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;

		// createEditText(9, 13);
		// this.invalidate();
	}

	public CrosswordLayout(Context context, PlayGame playGame) {
		super(context);
		this.context = context;
		this.playGame = playGame;

//		row = playGame.row;
//		column = playGame.column;

		if (MainPlayActivity.column == 0)			
			return;
		
		widthCell = MainPlayActivity.widthScreen / MainPlayActivity.column;

		createEditText(MainPlayActivity.row, MainPlayActivity.column);
		// setLayoutParams(new LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
		// setBackgroundColor(color.brown);
		Log.i(tag, "CrosswordLayout() row, column:" + MainPlayActivity.row + "," + MainPlayActivity.column);
	}

	public void createEditText(int row, int column) {
		int i, j;
		int id = 0;
		ExtEditText extEdit;
		TableRow tbRow;
		// row.setMinimumHeight(42);
		Log.i(tag, "createEditText...");
		// LayoutParams layoutparam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		try {
			for (i = 0; i < row; i++) {
				tbRow = new TableRow(this.getContext());
				tbRow.setLayoutParams(new LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
						android.widget.TableRow.LayoutParams.WRAP_CONTENT));
				tbRow.setPadding(0, 1, 0, 0);
				// tbRow.setLayoutParams(layoutparam);
				for (j = 0; j < column; j++) {
					id++;
					extEdit = new ExtEditText(context);
					extEdit.setId(id);
					// extEdit.setText(id+"");
					// extEdit.setTextColor(color.brown);
					// Hide EditText
					extEdit.setVisibility(View.INVISIBLE);

					// Set layout_width and layout height
					extEdit.setLayoutParams(new android.widget.TableRow.LayoutParams(widthCell, widthCell));

					// Set length of EditText
					InputFilter[] FilterArray = new InputFilter[1];
					FilterArray[0] = new InputFilter.LengthFilter(1);
					extEdit.setFilters(FilterArray);

					extEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
						public void onFocusChange(View v, boolean hasFocus) {
							if (hasFocus) {
								ExtEditText extE = (ExtEditText) v;
								touchEdit.SetOnTouch(extE.getnumEdt());
							}
						}
					});

					tbRow.addView(extEdit);

				}
				// this.addView(tbRow);
				this.addView(tbRow, new TableLayout.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
						android.widget.TableRow.LayoutParams.WRAP_CONTENT));

			}

			// Load data to GridView
			loadGird(false);
			// loadGird(false);

		} catch (Exception ex) {
			Log.e(tag, "createEditText() Error: " + ex);
		}
	}

	public void loadGird(boolean text) {
		int id;
		int i = 0, j = 0;
		int nId = 0;
		Log.i(tag, "loadGrid() text:" + text);
		try {
			// clearGrid();
			for (clsWordDetail wd : playGame.arrList) {
				char[] chWords = wd.getWords().toCharArray();
				if (wd.isAcross()) {
					for (i = 0; i < chWords.length; i++) {
						ExtEditText edt = (ExtEditText) this.findViewById(wd.getPos() + i + nId);
						edt.setAcross(wd.isAcross());
						edt.setNumEdt(wd.getNum());
						edt.setVisibility(View.VISIBLE);
						if (text)
							edt.setText(chWords[i] + "");
						if (i == 0) {
							// Log.i(tag, "num:" + wd.getNum() + "; Pos: " + wd.getPos() + "; id: " + edt.getId() +"; getText: " + edt.getText());
							edt.setNumEdtShow(wd.getNum());
							edt.setPos(wd.getPos());
						}
					}

				} else {
					for (j = 0; j < chWords.length; j++) {
						id = wd.getPos() + MainPlayActivity.column * j;
						if (id < MainPlayActivity.row * MainPlayActivity.column) {
							ExtEditText edt = (ExtEditText) this.findViewById(id + nId);
							// Log.i(tag, "num:" + wd.getNum() + "; Pos: " + wd.getPos() + "; id: " + edt.getId());
							edt.setchar(chWords.toString());
							edt.setAcross(wd.isAcross());
							edt.setNumEdt(wd.getNum());
							edt.setVisibility(View.VISIBLE);
							if (text)
								edt.setText(chWords[j] + "");
							if (j == 0) {
								edt.setNumEdtShow(wd.getNum());
								edt.setPos(wd.getPos());
							}
						}

					}
				}
			}

			// draw number

		} catch (Exception ex) {
			Log.e(tag, "loadGird() i,j=" + i + "," + j + "; Error: " + ex.getMessage());
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		// return super.onTouchEvent(event);
		// Log.i(tag, "Crd onTouchEvent..................");
		return false;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		// Log.i(TAG, "on draw");// Khong chay duoc den phuong thuc onDraw ???
	}

	public interface OnTouchEditText {
		public void SetOnTouch(int num);
	}

	public void SetOnTouchEdit(OnTouchEditText touchEdit) {
		this.touchEdit = touchEdit;
	}

}
